package com.ja.openchat.http;

import android.app.Application;

import com.alibaba.fastjson.JSONObject;
import com.ja.openchat.api.ApiUtils;
import com.ja.openchat.http.utils.Utils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cookie.CookieJarImpl;
import com.lzy.okgo.cookie.store.MemoryCookieStore;
import com.lzy.okgo.https.HttpsUtils;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;
import com.zy.openchat.App;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class HttpVueClient {

    private static final int TIMEOUT = 10000;
    private static final int PAGE_NUM = 20;
    private static HttpVueClient sInstance;
    private OkHttpClient mOkHttpClient;
    private String mUrl;
    private String token = "";
    private HttpVueClient() {
    }

    public static HttpVueClient getInstance() {
        if (sInstance == null) {
            synchronized (HttpVueClient.class) {
                if (sInstance == null) {
                    sInstance = new HttpVueClient();
                }
            }
        }
        return sInstance;
    }

    public void init(Application sInstance, final String host) {
        //mUrl = host + "api/";
        mUrl = host;
        Utils.init(sInstance);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(1000, TimeUnit.MILLISECONDS);
        builder.readTimeout(TIMEOUT, TimeUnit.MILLISECONDS);
        builder.writeTimeout(TIMEOUT, TimeUnit.MILLISECONDS);
        builder.cookieJar(new CookieJarImpl(new MemoryCookieStore()));
        builder.retryOnConnectionFailure(true);
        //输出HTTP请求 响应信息
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("http");
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BASIC);
        builder.addInterceptor(loggingInterceptor);
        builder.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request()
                        .newBuilder()
                        .addHeader("referer", host)
                        .addHeader("Content-Type", "application/json")
                        .addHeader("Accept", "application/json")
                        .addHeader("Authorization", token)
                        .build();
                return chain.proceed(request);
            }
        });
        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory();
        builder.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                .hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                });
        builder.addNetworkInterceptor(new HttpCacheInterceptor());
        mOkHttpClient = builder.build();
        OkGo.getInstance().init(sInstance).setOkHttpClient(mOkHttpClient);
    }

    public GetRequest<JSONObject> get(String serviceName, String tag) {
        return OkGo.<JSONObject>get(mUrl + serviceName)
                .tag(tag);
    }

    public PostRequest<JSONObject> post(String serviceName, String tag) {
        return OkGo.<JSONObject>post(mUrl + serviceName)
                .tag(tag);
    }

    public PostRequest<JSONObject> postPage(String serviceName, String tag, int p) {
        p = p <= 0 ? 1 : p;
        return OkGo.<JSONObject>post(mUrl + serviceName)
                .params("pageSize", PAGE_NUM)
                .params("pageNum", p)
                .tag(tag);
    }

    public GetRequest<JSONObject> getPage(String serviceName, String tag, int p) {
        return OkGo.<JSONObject>get(mUrl + serviceName)
                .params("pageSize", PAGE_NUM)
                .params("pageNum", p)
                .tag(tag);
    }

    public PostRequest<JSONObject> postIM(String service, String tag) {
        return OkGo.<JSONObject>post(service)
                .tag(tag);
    }
    public PostRequest<JSONObject> postRetryCount(String serviceName, String tag) {
        OkGo.getInstance().setRetryCount(0);
        return OkGo.<JSONObject>post(mUrl + serviceName)
                .tag(tag);
    }
    public GetRequest<JSONObject> getIM(String service, String tag) {
        return OkGo.<JSONObject>get(service)
                .tag(tag);
    }

    public void cancel(String tag) {
        OkGo.cancelTag(mOkHttpClient, tag);
    }
}
